﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIPO.Library.DomainObject.Administration;
using SIPO.Library.DataAccess.Administration;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Transaction;
using System.Configuration;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;

using SIPO.Web.Support;
using SIPO.Library.DomainObject.Administration.UserManagement;

namespace SIPO.Web.Forms.Transaction
{
   public partial class recInvoiceform : BasePage
    {
        private string id = string.Empty;

        recSectorRepo recSectorDal = new recSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        recSubSectorRepo recSubSectorDal = new recSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        recSubSubSectorRepo recSubSubSectorDal = new recSubSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

        protected override void OnLoad(EventArgs e)
        {

            ////populate dropdownlist sector
            //List<recSector> sectors = recSectorDal.GetListBySubSubSectorIDs(base.SubSubSectorIDs);
            //ddlSector.Items.Clear();
            //sectors.ForEach(delegate(recSector sector)
            //{
            //    ddlSector.Items.Add(new ListItem(sector.SectorCode + "-" + sector.SectorName, sector.SectorID.ToString()));
            //});
            //ddlSector.Items.Insert(0, new ListItem("-- pilih sektor --", string.Empty));

            ////populate dropdownlist Sub sector
            //ddlSubSector.Items.Insert(0, new ListItem("-- pilih sub sektor --", string.Empty));

            ////populate dropdownlist Sub sector
            //ddlSubSubSector.Items.Insert(0, new ListItem("-- pilih sub sub sector --", string.Empty));


            if (Request.Params["id"] != null)
            {
                id = Request.Params["id"];
            }

            txtInvoiceDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            if (!Page.IsPostBack)
            {
                if (id.Trim().Length > 0) // Edit an existing item
                {
                    recInvoiceRepo recInvoiceDal = new recInvoiceRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

                    recInvoice obj = recInvoiceDal.GetById(id.Trim());
                    txtInvoiceDate.Text = obj.InvoiceDate.ToString("dd/MM/yyyy");
                   
                }

                GetPrivilege();

                if (base.DebtorID == null || base.DebtorID == 0)
                {
                    var url = Helper.GetAppVirtualPath(Page);
                    ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda login bukan sebagai debitur tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                }


            }

        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').hide();", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').show();", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').show();", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }
    
    }
}
